<template>
  <div class="roleManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="roleName"
        label="角色名称"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleMap(row)">映射菜单</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <edit ref="edit" @fetch-data="fetchData"></edit>
    <role-map-edit ref="roleEdit" @fetch-data="fetchData"></role-map-edit>
  </div>
</template>

<script>
  import { getAllAdminRole, deleteAdminRole } from '@/api/roleManagement'
  import Edit from './components/RoleManagementEdit'
  import RoleMapEdit from './components/RoleMapEdit.vue'

  export default {
    name: 'RoleManagement',
    components: { Edit, RoleMapEdit },
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          permission: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      handleMap(row) {
        if (row.id) {
          this.$refs['roleEdit'].showEdit(row)
        } else {
          this.$refs['roleEdit'].showEdit()
        }
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const { data } = await deleteAdminRole(row)
            if (data.status === 0) {
              this.$baseMessage(data.message, 'success')
              this.fetchData()
            } else {
              this.$baseMessage(data.message, 'error')
            }
          })
        }
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await getAllAdminRole()
        const { body } = data
        this.list = body
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
